package com.longlong.pool;

import java.sql.*;

/**
 * Connection 封装类
 *
 * @author longlong
 */
public class MyConnection extends AbstractConnection{

    /**
     * 数据库连接
     */
    private Connection connection;

    /**
     * 连接的状态 false 空闲 true 占用
     */
    private boolean used = false;

    /**
     * 获取配置信息
     */
    private static String driver = ConfigurationReader.getStringValue("driver");
    private static String url = ConfigurationReader.getStringValue("url");
    private static String username = ConfigurationReader.getStringValue("username");
    private static String password = ConfigurationReader.getStringValue("password");

    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    {
        try {
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() {
        return connection;
    }

    public boolean isUsed() {
        return used;
    }

    public void setUsed(boolean used) {
        this.used = used;
    }

    @Override
    public Statement createStatement() throws SQLException {
        return this.connection.createStatement();
    }
    @Override
    public PreparedStatement prepareStatement(String sql) throws SQLException {
        PreparedStatement pstat = this.connection.prepareStatement(sql);
        return pstat;
    }
    @Override
    public void close() throws SQLException {
        this.used = false;
    }

}
